package com.ycz.controller;


import com.ycz.common.Result;
import com.ycz.entity.TArticle;
import com.ycz.service.TArticleService;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FileUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ycz
 * @since 2021-09-25
 */
@Controller
@RequestMapping("/article")
public class TArticleController {

    @Autowired
    private TArticleService tArticleService;

    @Resource
    HttpServletRequest request;

//    @PostMapping("/addBlog")
//    public String addBlog(String email, String content, String title, String name, MultipartFile image) throws IOException {
//        System.out.println(email+content+title+name);
//
//        if(!image.isEmpty()){
//            //文件上传的地址
//            String path = ResourceUtils.getURL("classpath:").getPath()+"static/images";
//            String realPath = path.replace('/', '\\').substring(1,path.length());
//            System.out.println(realPath);
//
//            //获取文件的名称
//            final String fileName = image.getOriginalFilename();
//            String contentType = image.getContentType();
//
//            if("image/jpeg".equals(contentType) || "image/jpg".equals(contentType) ){
//                File file = new File(realPath,fileName);
//
//                //完成文件的上传
//                image.transferTo(file);
//                System.out.println("图片上传成功!");
//                String path01 = "images/"+fileName;
//                System.out.println(path01);
//                TArticle tArticle=new TArticle();
//                tArticle.setTags(title);
//                tArticle.setTitle(title);
//                tArticle.setContent(content);
//                tArticle.setImage(path01);
//                tArticleService.save(tArticle);
//            } else {
//                System.out.println("上传失败！");
//            }
//        }
//        return "redirect:/blog/index";
//    }

//    @PostMapping("/addBlog")
//    public String addBlog2(String email, String content, String title, String name, MultipartFile image) throws IOException {
//        System.out.println(email+content+title+name);
//
//
//
//        String fileName = image.getOriginalFilename();
//        //设置文件上传路径
//        String filePath = request.getSession().getServletContext().getRealPath("images/");
//        try {
//            FileUtil.uploadFile(image.getBytes(), filePath, fileName);
//            TArticle tArticle=new TArticle();
//                tArticle.setTags(title);
//                tArticle.setTitle(title);
//                tArticle.setContent(content);
//                tArticle.setImage(filePath+fileName);
//                tArticleService.save(tArticle);
//        } catch (Exception e) {
//        }
//        return "redirect:/blog/index";
//    }

    @PostMapping("/addBlog")
    public String addBlog3(String email, String content, String title, String name,
                           MultipartFile image) throws IOException {
//图片名称
        //        System.out.println(email+content+title+name);
        System.out.println("进来了");
        String originalFilename=image.getOriginalFilename();
        //获取图片的后缀
        String extName=FileUtil.extName(originalFilename);
        System.out.println(extName);
        //产生图片的名称
        String newFileName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        newFileName=newFileName+"."+extName;

        //获取upload文件夹的物理路径
        String realPath = request.getSession().getServletContext().getRealPath("images");

        System.out.println(realPath);
        //文件保存的物理路径               文件分隔符，window和Linux的分割不一样
        String fileRealPath=realPath+ File.separator+ newFileName;
        //文件还需要url路径 upload/xxxx.jpg

        String url="images"+"/"+newFileName;

        System.out.println(url);

        FileUtils.copyInputStreamToFile(image.getInputStream(), new File(fileRealPath));
                TArticle tArticle=new TArticle();
                tArticle.setTags(title);
                tArticle.setTitle(title);
                tArticle.setContent(content);
                tArticle.setImage(url);
                tArticleService.save(tArticle);

        return "redirect:/blog/index";

    }


    @RequestMapping({"/blog/single"})
    @ResponseBody
    public Object toBlogSingle(Model model, Integer id){

        System.out.println("single"+id);

        return new Result();
    }
}

